<?php

function get_permission_table(){
	global $conf;

	$db = new dbquery;
	//wrzucanie do tablicy tabeli uprawnien
	$db->cache('groups_pdefs', 'groups');
	$db->query("SELECT name, defs, types FROM $conf[prefix]groups_pdefs");
	while($d=$db->fetch_object()){
		$GLOBALS['groups_pdefs_'.$d->name.'_defs']=$d->defs;
		$GLOBALS['groups_pdefs_'.$d->name.'_types']=$d->types;
	}
	//

	//wrzucanie do tablicy uprawnien danego usera
	if(!$_SESSION['id'])
	$GLOBALS['gid']=1;
	else
	$GLOBALS['gid']=get_gid_by_uid($_SESSION['id']);

	$db->cache();
	$db->query("SELECT * FROM $conf[prefix]groups WHERE id=$GLOBALS[gid]");
	while($d=$db->fetch_object()){
		for($i=0;$i<$db->num_fields();$i++) {
			$c=$db->fetch_field($i);
			$t=$c->name;
			$GLOBALS['group_'.$t]=$d->$t;
		}
	}
	//
}

function perms_check($module, $what) {
	if($module == NULL or $what == NULL)
		return false;
	else {
		$defs = explode(';', $GLOBALS['groups_pdefs_'.$module.'_defs']);
		$types = explode(';', $GLOBALS['groups_pdefs_'.$module.'_types']);

		for($i = 0; $i < count($defs); $i++) {
			if($defs[$i] == $what)
				break;
		}

		if($defs[$i] != $what)
			return false;

		$values = explode(';', $GLOBALS['group_'.$module]);

		if($types[$i] == 'bool') {
			if($values[$i] == 1)
				return true;
			else
				return false;
		}
		else
			return $values[$i];
	}
}

function get_gid_by_uid($id_) {
	global $conf;

	SQLvalidate($id_);

	if($id_==$_SESSION['id'] && $GLOBALS['gid'])
	return $GLOBALS['gid'];

	$db = new dbquery;
	$db->cache('user_by_id_'.$id_, 'users');
	$db->clear_fields(array('pass', 'buddies'));
	$db->query("SELECT * FROM $conf[prefix_users]users WHERE id=$id_") or $db->err(__FILE__, __LINE__);
	$d = $db->fetch_object(true);

	return $d->gid;

}

function get_group_info_by_gid($gid_){
	global $conf;

	SQLvalidate($gid_);
	if($gid_) {
		$db = new dbquery;
		$db->cache('group_'.$gid_, 'groups');
		$db->query("SELECT * FROM $conf[prefix_groups]groups WHERE id = $gid_") or $db->err(__FILE__, __LINE__);

		$group=$db->fetch_object(true);
		$nm = 'name_'.$_SESSION['lang_short'];

		if($group->$nm == '') {
			$nm = 'name_'.$conf['language'];
		}

		$group->name = read_text_rest($group->$nm);

		return $group;
	}
}

function get_group_type($gid_) {
	global $conf;

	SQLvalidate($gid_);

	if(!$gid_) return false;
	
	if($gid_==$GLOBALS['gid'])
	return $GLOBALS['group']['type'];

	$db = new dbquery;
	$db->cache('group_'.$gid_, 'groups');
	$db->query("SELECT * FROM $conf[prefix_groups]groups WHERE id = $gid_") or $db->err(__FILE__, __LINE__);
	$d = $db->fetch_object(true);

	return $d->type;
}

//funkcja zwraca tablic� gid grup redaktorow 
function get_redactor_gids() {
	global $conf;
	
	$db=new dbquery;
	$db->query("SELECT * FROM $conf[prefix]groups WHERE type=2");
	
	$arr=array();
	while($d=$db->fetch_object()) $arr[]=$d->id;
	
	$arr[]=3; //add admin group
	
	return $arr;
}
//

function groups_get_options($gr = NULL) {
	global $conf;

	$db = new dbquery;

	$options = NULL;

	$db->query("SELECT * FROM $conf[prefix_groups]groups WHERE id = 3") or $db->err(__FILE__, __LINE__);

	$d = $db->fetch_object();

	$nn = NULL;
	if($gr == 3)
	$nn =' SELECTED';

	$nm = "name_$_SESSION[lang_short]";

	if($d->$nm == '') {
		$nm = "name_".$conf['language'];
	}

	$group = read_text_rest($d->$nm);

	$options .= "<option value=\"3\"$nn>$group</option>\n";

	$db->query("SELECT * FROM $conf[prefix_groups]groups WHERE type != -1 ORDER BY type DESC") or $db->err(__FILE__, __LINE__);

	while($d = $db->fetch_object()) {
		$nn = NULL;
		if($gr == $d->id)
		$nn =' selected';

		$nm = "name_$_SESSION[lang_short]";

		if($d->$nm == '') {
			$nm = "name_".$conf['language'];
		}

		$group = read_text_rest($d->$nm);

		$options .= "<option value=\"$d->id\"$nn>$group</option>\n";
	}

	$db->query("SELECT * FROM $conf[prefix_groups]groups WHERE id = 2") or $db->err(__FILE__, __LINE__);

	$d = $db->fetch_object();

	$nn = NULL;
	if($gr == 2)
	$nn =' selected';

	$nm = "name_$_SESSION[lang_short]";

	if($d->$nm == '') {
		$nm = "name_".$conf['language'];
	}

	$group = read_text_rest($d->$nm);

	$options .= "<option value=\"2\"$nn>$group</option>\n";

	return $options;
}

function groups_get_name_by_id($gid_) {
	global $conf;

	if(!$gid_)
	return FALSE;
	else {
		$db = new dbquery;
		$db->cache('group_'.$gid_, 'groups');
		$db->query("SELECT * FROM $conf[prefix_groups]groups WHERE id = $gid_") or $db->err(__FILE__, __LINE__);
		
		if($db->num_rows() == 0)
		return FALSE;
		else {
			$group = $db->fetch_object(true);

			$nm = 'name_'.$_SESSION['lang_short'];

			if($group->$nm == '')
			$nm = 'name_'.$conf['language'];

			return read_text_rest($group->$nm);
		}
	}
}

function groups_get_color_by_id($gid_) {
	global $conf;

	if(!$gid_)
	return FALSE;
	else {
		$db = new dbquery;
		$db->cache('group_'.$gid_, 'groups');
		$db->query("SELECT * FROM $conf[prefix_groups]groups WHERE id = $gid_") or $db->err(__FILE__, __LINE__);

		if($db->num_rows() == 0)
		return FALSE;
		else {
			$group = $db->fetch_object(true);
			return read_text_rest($group->color);
		}
	}
}

function group_add_perms($module='', $defs, $types, $default_perms, $admin_perms, $default_names, $default_desc, $pl_names='', $pl_desc='') {
	global $conf;
	if(!$module) exit;

	if(perms_check('admin', 'modules')) {
		
		$sql=array();
		$languages = get_lang_list();
		foreach($languages as $l) {
			if($l=='pl' && $pl_names && $pl_desc)
				$sql[]="'$pl_names', '$pl_desc'";
			else
				$sql[]="'$default_names', '$default_desc'";			
		}
			
		$db=new dbquery;
		$db->query("INSERT INTO $conf[prefix]groups_pdefs VALUES('".$module."', '".$defs."', '".$types."', ".implode(", ", $sql).")");
		$db->query("ALTER TABLE $conf[prefix]groups ADD `".$module."` VARCHAR(30) NULL DEFAULT '".$default_perms."' AFTER `rank`");
		$db->query("UPDATE $conf[prefix]groups SET ".$module."='".$admin_perms."' WHERE id=3");
		
		$db->cache('*', 'groups');
	}
}

function group_remove_perms($module='') {
	global $conf;
	if(!$module) exit;
	
	if(perms_check('admin', 'modules')) {
		$db=new dbquery;
		$db->query("DELETE FROM $conf[prefix]groups_pdefs WHERE name='".$module."'") or $db->err(__FILE__, __LINE__);
		$db->query("ALTER TABLE $conf[prefix]groups DROP `".$module."`") or $db->err(__FILE__, __LINE__);
		
		$db->cache('*', 'groups');
	}
}
?>
